package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.DAO.*;
import com.ruoyi.StarStudio.Entity.*;
import com.ruoyi.StarStudio.Service.OrderService;
import com.ruoyi.StarStudio.Utils.HttpRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrdersDAO ordersDAO;

    @Autowired
    GoodsDAO goodsDAO;

    @Autowired
    GoodsStockDAO goodsStockDAO;

    @Autowired
    CouponsDAO couponsDAO;

    @Autowired
    CouponsStockDAO couponsStockDAO;

    @Autowired
    UserInfoDAO userInfoDAO;

    @Autowired
    BeansActionDAO beansActionDAO;

    @Autowired
    ExchangeDAO exchangeDAO;

    @Override
    public List<Orders> findOrdersList(Orders orders) {
        if (getLoginUser().getUser().isAdmin()){
            return ordersDAO.findOrdersList(orders);
        }
        return ordersDAO.findOrdersListByNormalStore(orders,getLoginUser().getUserId());
    }

    @Override
    public long findAllRecordNumber() {
        if (getLoginUser().getUser().isAdmin()){
            return ordersDAO.findAllRecordForAdmin();
        }
        return ordersDAO.findAllRecordForNormalStore(getLoginUser().getUserId());
    }

    /**
     * 新订单
     * @param orders 订单对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void NewOrder(Orders orders) {
        if (orders.getGoods_id() > 0){ /* 实体商品 */
            //减少商品库存  标识实体商品库存项的属性为已售卖  标识订单ID
            Goods byGoods_id = goodsDAO.findByGoods_id(orders.getGoods_id());
            if (byGoods_id.getStock() >= orders.getNumber()){
                orders.setOrder_status(MessageConstant.ORDER_ALREADY_PAID);//更新订单状态
                orders.setOrder_time(new Timestamp(new Date().getTime())); //添加下单时间
                ordersDAO.orderAdd(orders);
                long order_id = orders.getOrder_id();
                byGoods_id.setStock(byGoods_id.getStock() - orders.getNumber());
                goodsDAO.updateGoods(byGoods_id);
                List<GoodsStock> unsoldItems = goodsStockDAO.findUnsoldItems(orders.getGoods_id(), false);
                if (unsoldItems.size() > 0){
                    for (int i = 0; i < orders.getNumber(); i++) {
                        // 往 GoodsStock中更新数据
                        GoodsStock goodsStock = unsoldItems.get(i);
                        goodsStock.setHave_been_sold(true);
                        goodsStock.setSold_order_id(order_id);
                        goodsStock.setSold_time(new Timestamp(new Date().getTime()));
                        goodsStockDAO.updateGoodsStock(goodsStock);
                        // 往 Exchange中更新数据
                        Exchange exchange = new Exchange();
                        exchange.setExchange_status(MessageConstant.EXCHANGE_STATUS_USED);
                        exchange.setOrder_id(order_id);
                        exchange.setExchange_user_id(orders.getOrder_user_id());
                        exchange.setGoods_item_id(goodsStock.getItem_id());
                        exchange.setUse_time(new Timestamp(new Date().getTime()));
                        exchangeDAO.addExchange(exchange);
                    }
                }
                //往 UserInfo中更新数据
                UserInfo byUser_id = userInfoDAO.findByUser_id(orders.getOrder_user_id());
                int goods_price = byGoods_id.getGoods_price();
                byUser_id.setBeans_spend_day(byUser_id.getBeans_spend_day() + goods_price * orders.getNumber());//更新该用户今天消费的鲜豆数
                byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() + goods_price * orders.getNumber());//更新该用户消费的总鲜豆数
                byUser_id.setBeans(byUser_id.getBeans() - goods_price * orders.getNumber());//扣除后更新用户拥有的鲜豆
                userInfoDAO.updateUserInfo(byUser_id);
                //往 BeansAction中更新数据
                BeansAction beansAction = new BeansAction();
                beansAction.setBeans_action_describe("兑换" + byGoods_id.getGoods_name());
                beansAction.setBeans_action_number(- goods_price * orders.getNumber());
                beansAction.setBeans_action_type(MessageConstant.BEANS_EXPEND);
                beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
                beansAction.setBeans_action_user_id(orders.getOrder_user_id());
                beansActionDAO.addBeansAction(beansAction);
                // 向省平台发送请求更新用户鲜豆
                HttpRequestUtils.ProvinceSystemBeanAction(orders.getOrder_user_id(),-(goods_price * orders.getNumber()));
            }else {
                throw new RuntimeException("商品库存不足！");
            }
        }else if (orders.getCoupons_id() > 0){ /* 优惠券 */
            //减少优惠券库存  标识优惠券库存项的属性为已售卖  标识订单ID
            Coupons byCoupon_id = couponsDAO.findByCoupon_id(orders.getCoupons_id());
            if (byCoupon_id.getStock() >= orders.getNumber()){
                orders.setOrder_status(MessageConstant.ORDER_FINISHED);//更新订单状态
                orders.setOrder_time(new Timestamp(new Date().getTime())); //添加下单时间
                ordersDAO.orderAdd(orders);
                long order_id = orders.getOrder_id();
                byCoupon_id.setStock(byCoupon_id.getStock() - orders.getNumber());
                couponsDAO.updateCoupons(byCoupon_id);
                List<CouponsStock> unsoldItems = couponsStockDAO.findUnsoldItems(orders.getCoupons_id(), false);
                if (unsoldItems.size() > 0){
                    for (int i = 0; i < orders.getNumber(); i++) {
                        // 往 CouponsStock中更新数据
                        CouponsStock couponsStock = unsoldItems.get(i);
                        couponsStock.setSold_order_id(order_id);
                        couponsStock.setHave_been_sold(true);
                        couponsStock.setSold_time(new Timestamp(new Date().getTime()));
                        couponsStockDAO.updateCouponsStock(couponsStock);
                        // 往 Exchange中更新数据
                        Exchange exchange = new Exchange();
                        exchange.setExchange_status(MessageConstant.EXCHANGE_STATUS_EXCHANGED_BUT_NOT_USED);
                        exchange.setOrder_id(order_id);
                        exchange.setExchange_user_id(orders.getOrder_user_id());
                        exchange.setCoupons_item_id(couponsStock.getItem_id());
                        exchangeDAO.addExchange(exchange);
                    }
                }
                //往 UserInfo中更新数据
                UserInfo byUser_id = userInfoDAO.findByUser_id(orders.getOrder_user_id());
                int coupon_price = byCoupon_id.getCoupon_price();
                double market_price = byCoupon_id.getMarket_price();
                byUser_id.setBeans_spend_day(byUser_id.getBeans_spend_day() + coupon_price * orders.getNumber());//更新该用户今天消费的鲜豆数
                byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() + coupon_price * orders.getNumber());//更新该用户消费的总鲜豆数
                byUser_id.setCoupons_spend_day(byUser_id.getCoupons_spend_day() + orders.getNumber());//更新该用户今天消费的优惠券数量
                byUser_id.setCoupons_value_spend_day(byUser_id.getCoupons_value_spend_day() + market_price * orders.getNumber());//更新该用户今天消费的优惠券总额度
                byUser_id.setCoupons_spend_total(byUser_id.getCoupons_spend_total() + orders.getNumber());//更新该用户在商城消费的总优惠券数量
                byUser_id.setCoupons_value_spend_total(byUser_id.getCoupons_spend_total() + market_price * orders.getNumber());//更新该用户在商城消费的优惠券总额度
                byUser_id.setBeans(byUser_id.getBeans() - coupon_price * orders.getNumber());//扣除后更新用户剩余鲜豆数
                userInfoDAO.updateUserInfo(byUser_id);
                //往 BeansAction中更新数据
                BeansAction beansAction = new BeansAction();
                beansAction.setBeans_action_describe("兑换" + byCoupon_id.getCoupon_name());
                beansAction.setBeans_action_number(- coupon_price * orders.getNumber());
                beansAction.setBeans_action_type(MessageConstant.BEANS_EXPEND);
                beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
                beansAction.setBeans_action_user_id(orders.getOrder_user_id());
                beansActionDAO.addBeansAction(beansAction);
                // 向省平台发送请求更新用户鲜豆
                HttpRequestUtils.ProvinceSystemBeanAction(orders.getOrder_user_id(),-(coupon_price * orders.getNumber()));
            }else {
                throw new RuntimeException("优惠券库存不足！");
            }
        }
    }

    /**
     * 修改订单
     * @param orders 订单对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void UpdateOrder(Orders orders) {
        Orders OldOrder = ordersDAO.findByOrder_id(orders.getOrder_id());
        if (orders.getGoods_id() > 0){  /* 实体商品 */
            //涉及到修改订单中个体数量的处理
            if (orders.getNumber() > OldOrder.getNumber()){ //数量增加
                Goods byGoods_id = goodsDAO.findByGoods_id(orders.getGoods_id());
                if (byGoods_id.getStock() >= (orders.getNumber() - OldOrder.getNumber())){ //新增数量在库存范围之内
                    byGoods_id.setStock(byGoods_id.getStock() - (orders.getNumber() - OldOrder.getNumber()));
                    goodsDAO.updateGoods(byGoods_id);
                    List<GoodsStock> unsoldItems = goodsStockDAO.findUnsoldItems(orders.getGoods_id(), false);
                    if (unsoldItems.size() > 0){
                        for (int i = 0; i < (orders.getNumber() - OldOrder.getNumber()); i++) {
                            // 往 GoodsStock中更新数据
                            GoodsStock goodsStock = unsoldItems.get(i);
                            goodsStock.setHave_been_sold(true);
                            goodsStock.setSold_order_id(orders.getOrder_id());
                            goodsStock.setSold_time(new Timestamp(new Date().getTime()));
                            goodsStockDAO.updateGoodsStock(goodsStock);
                            // 往 Exchange中更新数据
                            Exchange exchange = new Exchange();
                            exchange.setExchange_status(MessageConstant.EXCHANGE_STATUS_USED);
                            exchange.setOrder_id(orders.getOrder_id());
                            exchange.setExchange_user_id(orders.getOrder_user_id());
                            exchange.setGoods_item_id(goodsStock.getItem_id());
                            exchange.setUse_time(new Timestamp(new Date().getTime()));
                            exchangeDAO.addExchange(exchange);
                        }
                    }
                    // 往 UserInfo中更新数据
                    // 注：修改订单商品数量会导致用户鲜豆数发生变更，商家务必在征得用户同意后更改商品数量！
                    UserInfo byUser_id = userInfoDAO.findByUser_id(orders.getOrder_user_id());
                    int goods_price = byGoods_id.getGoods_price();
                    byUser_id.setBeans_spend_day(byUser_id.getBeans_spend_day() + goods_price * (orders.getNumber() - OldOrder.getNumber()));//更新该用户今天消费的鲜豆数
                    byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() + goods_price * (orders.getNumber() - OldOrder.getNumber()));//更新该用户消费的总鲜豆数
                    byUser_id.setBeans(byUser_id.getBeans() - goods_price * (orders.getNumber() - OldOrder.getNumber()));//扣除后更新用户拥有的鲜豆
                    userInfoDAO.updateUserInfo(byUser_id);
                    //往 BeansAction中更新数据
                    BeansAction beansAction = new BeansAction();
                    beansAction.setBeans_action_describe("兑换" + byGoods_id.getGoods_name() + "时增加订单中的商品数量，平台扣除。");
                    beansAction.setBeans_action_number(- goods_price * (orders.getNumber() - OldOrder.getNumber()));
                    beansAction.setBeans_action_type(MessageConstant.BEANS_EXPEND);
                    beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
                    beansAction.setBeans_action_user_id(orders.getOrder_user_id());
                    beansActionDAO.addBeansAction(beansAction);
                    // 向省平台发送请求更新用户鲜豆
                    HttpRequestUtils.ProvinceSystemBeanAction(orders.getOrder_user_id(),-(goods_price * (orders.getNumber() - OldOrder.getNumber())));
                }else {
                    throw new RuntimeException("商品库存不足！");
                }
            }else if (orders.getNumber() < OldOrder.getNumber() && orders.getNumber() > 0){ //数量减少
                Goods byGoods_id = goodsDAO.findByGoods_id(orders.getGoods_id());
                byGoods_id.setStock(byGoods_id.getStock() + (OldOrder.getNumber() - orders.getNumber()));
                goodsDAO.updateGoods(byGoods_id);
                List<GoodsStock> soldItems = goodsStockDAO.findSoldItems(orders.getGoods_id(), true);
                if (soldItems.size() > 0){
                    for (int i = 0; i < (OldOrder.getNumber() - orders.getNumber()); i++) {
                        GoodsStock goodsStock = soldItems.get(i);
                        goodsStock.setSold_order_id(0);
                        goodsStock.setHave_been_sold(false);
                        goodsStockDAO.updateGoodsStock(goodsStock);
                        // 往 Exchange中更新数据
                        exchangeDAO.deleteExchangeByGoodsCondition(orders.getOrder_id(),goodsStock.getItem_id());
                    }
                }
                // 往 UserInfo中更新数据
                UserInfo byUser_id = userInfoDAO.findByUser_id(orders.getOrder_user_id());
                int goods_price = byGoods_id.getGoods_price();
                byUser_id.setBeans_spend_day(byUser_id.getBeans_spend_day() - goods_price * (OldOrder.getNumber() - orders.getNumber()));//更新该用户今天消费的鲜豆数
                byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() - goods_price * (OldOrder.getNumber() - orders.getNumber()));//更新该用户消费的总鲜豆数
                byUser_id.setBeans(byUser_id.getBeans() + goods_price * (OldOrder.getNumber() - orders.getNumber()));//扣除后更新用户拥有的鲜豆
                userInfoDAO.updateUserInfo(byUser_id);
                //往 BeansAction中更新数据
                BeansAction beansAction = new BeansAction();
                beansAction.setBeans_action_describe("兑换" + byGoods_id.getGoods_name() + "时减少订单商品数量，平台退还。");
                beansAction.setBeans_action_number(goods_price * (OldOrder.getNumber() - orders.getNumber()));
                beansAction.setBeans_action_type(MessageConstant.BEANS_INCOME);
                beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
                beansAction.setBeans_action_user_id(orders.getOrder_user_id());
                beansActionDAO.addBeansAction(beansAction);
                // 向省平台发送请求更新用户鲜豆
                HttpRequestUtils.ProvinceSystemBeanAction(orders.getOrder_user_id(), goods_price * (OldOrder.getNumber() - orders.getNumber()));
            }
        }else if (orders.getCoupons_id() > 0){  /* 优惠券 */
            //涉及到修改订单中个体数量的处理
            if (orders.getNumber() > OldOrder.getNumber()){ //数量增加
                Coupons byCoupon_id = couponsDAO.findByCoupon_id(orders.getCoupons_id());
                if (byCoupon_id.getStock() >= (orders.getNumber() - OldOrder.getNumber())){ //新增数量在库存范围之内
                    byCoupon_id.setStock(byCoupon_id.getStock() - (orders.getNumber() - OldOrder.getNumber()));
                    couponsDAO.updateCoupons(byCoupon_id);
                    List<CouponsStock> unsoldItems = couponsStockDAO.findUnsoldItems(orders.getGoods_id(), false);
                    for (int i = 0; i < (orders.getNumber() - OldOrder.getNumber()); i++) {
                        // 往 CouponsStock中更新数据
                        CouponsStock couponsStock = unsoldItems.get(i);
                        couponsStock.setHave_been_sold(true);
                        couponsStock.setSold_order_id(orders.getOrder_id());
                        couponsStock.setSold_time(new Timestamp(new Date().getTime()));
                        couponsStockDAO.updateCouponsStock(couponsStock);
                        // 往 Exchange中更新数据
                        Exchange exchange = new Exchange();
                        exchange.setExchange_status(MessageConstant.EXCHANGE_STATUS_EXCHANGED_BUT_NOT_USED);
                        exchange.setOrder_id(orders.getOrder_id());
                        exchange.setExchange_user_id(orders.getOrder_user_id());
                        exchange.setCoupons_item_id(couponsStock.getItem_id());
                        exchangeDAO.addExchange(exchange);
                    }
                    // 往 UserInfo中更新数据
                    UserInfo byUser_id = userInfoDAO.findByUser_id(orders.getOrder_user_id());
                    int coupon_price = byCoupon_id.getCoupon_price();
                    double market_price = byCoupon_id.getMarket_price();
                    byUser_id.setBeans_spend_day(byUser_id.getBeans_spend_day() + coupon_price * (orders.getNumber() - OldOrder.getNumber()));//更新该用户今天消费的鲜豆数
                    byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() + coupon_price * (orders.getNumber() - OldOrder.getNumber()));//更新该用户消费的总鲜豆数
                    byUser_id.setCoupons_spend_day(byUser_id.getCoupons_spend_day() + (orders.getNumber() - OldOrder.getNumber()));//更新该用户今天消费的优惠券数量
                    byUser_id.setCoupons_value_spend_day(byUser_id.getCoupons_value_spend_day() + market_price * (orders.getNumber() - OldOrder.getNumber()));//更新该用户今天消费的优惠券总额度
                    byUser_id.setCoupons_spend_total(byUser_id.getCoupons_spend_total() + (orders.getNumber() - OldOrder.getNumber()));//更新该用户在商城消费的总优惠券数量
                    byUser_id.setCoupons_value_spend_total(byUser_id.getCoupons_spend_total() + market_price * (orders.getNumber() - OldOrder.getNumber()));//更新该用户在商城消费的优惠券总额度
                    byUser_id.setBeans(byUser_id.getBeans() - coupon_price * (orders.getNumber() - OldOrder.getNumber()));//扣除后更新用户剩余鲜豆数
                    userInfoDAO.updateUserInfo(byUser_id);
                    //往 BeansAction中更新数据
                    BeansAction beansAction = new BeansAction();
                    beansAction.setBeans_action_describe("兑换" + byCoupon_id.getCoupon_name() + "时增加订单中的优惠券数量，平台扣除。");
                    beansAction.setBeans_action_number(- coupon_price * (orders.getNumber() - OldOrder.getNumber()));
                    beansAction.setBeans_action_type(MessageConstant.BEANS_EXPEND);
                    beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
                    beansAction.setBeans_action_user_id(orders.getOrder_user_id());
                    beansActionDAO.addBeansAction(beansAction);
                    // 向省平台发送请求更新用户鲜豆
                    HttpRequestUtils.ProvinceSystemBeanAction(orders.getOrder_user_id(),-(coupon_price * (orders.getNumber() - OldOrder.getNumber())));
                }else {
                    throw new RuntimeException("优惠券库存不足！");
                }
            }else if (orders.getNumber() < OldOrder.getNumber() && orders.getNumber() > 0){ //数量减少
                Coupons byCoupon_id = couponsDAO.findByCoupon_id(orders.getGoods_id());
                byCoupon_id.setStock(byCoupon_id.getStock() + (OldOrder.getNumber() - orders.getNumber()));
                couponsDAO.updateCoupons(byCoupon_id);
                List<CouponsStock> soldItems = couponsStockDAO.findSoldItems(orders.getCoupons_id(), true);
                for (int i = 0; i < (OldOrder.getNumber() - orders.getNumber()); i++) {
                    // 往 CouponsStock中更新数据
                    CouponsStock couponsStock = soldItems.get(i);
                    couponsStock.setSold_order_id(0);
                    couponsStock.setHave_been_sold(false);
                    couponsStockDAO.updateCouponsStock(couponsStock);
                    // 往 Exchange中更新数据
                    exchangeDAO.deleteExchangeByCouponsCondition(orders.getOrder_id(), couponsStock.getItem_id());
                }
                // 往 UserInfo中更新数据
                UserInfo byUser_id = userInfoDAO.findByUser_id(orders.getOrder_user_id());
                int coupon_price = byCoupon_id.getCoupon_price();
                double market_price = byCoupon_id.getMarket_price();
                byUser_id.setBeans_spend_day(byUser_id.getBeans_spend_day() - coupon_price * (OldOrder.getNumber() - orders.getNumber()));//更新该用户今天消费的鲜豆数
                byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() - coupon_price * (OldOrder.getNumber() - orders.getNumber()));//更新该用户消费的总鲜豆数
                byUser_id.setCoupons_spend_day(byUser_id.getCoupons_spend_day() - (OldOrder.getNumber() - orders.getNumber()));//更新该用户今天消费的优惠券数量
                byUser_id.setCoupons_value_spend_day(byUser_id.getCoupons_value_spend_day() - market_price * (OldOrder.getNumber() - orders.getNumber()));//更新该用户今天消费的优惠券总额度
                byUser_id.setCoupons_spend_total(byUser_id.getCoupons_spend_total() - (OldOrder.getNumber() - orders.getNumber()));//更新该用户在商城消费的总优惠券数量
                byUser_id.setCoupons_value_spend_total(byUser_id.getCoupons_spend_total() - market_price * (OldOrder.getNumber() - orders.getNumber()));//更新该用户在商城消费的优惠券总额度
                byUser_id.setBeans(byUser_id.getBeans() + coupon_price * (OldOrder.getNumber() - orders.getNumber()));//扣除后更新用户剩余鲜豆数
                userInfoDAO.updateUserInfo(byUser_id);
                //往 BeansAction中更新数据
                BeansAction beansAction = new BeansAction();
                beansAction.setBeans_action_describe("兑换" + byCoupon_id.getCoupon_name() + "时减少订单优惠券数量，平台退还。");
                beansAction.setBeans_action_number(coupon_price * (OldOrder.getNumber() - orders.getNumber()));
                beansAction.setBeans_action_type(MessageConstant.BEANS_INCOME);
                beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
                beansAction.setBeans_action_user_id(orders.getOrder_user_id());
                beansActionDAO.addBeansAction(beansAction);
                // 向省平台发送请求更新用户鲜豆
                HttpRequestUtils.ProvinceSystemBeanAction(orders.getOrder_user_id(),coupon_price * (OldOrder.getNumber() - orders.getNumber()));
            }
        }
        //修改订单表
        ordersDAO.updateOrder(orders);
    }

    /**
     * 根据订单ID取消订单
     * @param order_id 订单ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void CancelOrder(long order_id) {
        Orders byOrder_id = ordersDAO.findByOrder_id(order_id);
        if (byOrder_id.getGoods_id() > 0 && byOrder_id.getOrder_status().equals(MessageConstant.ORDER_ALREADY_PAID)){ /* 实体商品 */
            List<GoodsStock> goodsStocks = goodsStockDAO.findByOrder_id(order_id);
            if (goodsStocks.size() > 0){
                for (GoodsStock goodsStock : goodsStocks) {
                    // 向 GoodsStock中更新数据
                    goodsStock.setHave_been_sold(false);
                    goodsStock.setSold_order_id(0);
                    goodsStock.setSold_time(null);
                    goodsStockDAO.updateGoodsStock(goodsStock);
                    // 向 Exchange中更新数据
                    exchangeDAO.deleteExchangeByGoodsCondition(order_id,goodsStock.getItem_id());
                }
            }
            Goods byGoods_id = goodsDAO.findByGoods_id(byOrder_id.getGoods_id());
            byGoods_id.setStock(byGoods_id.getStock() + goodsStocks.size());
            goodsDAO.updateGoods(byGoods_id);
            //退还鲜豆给用户  更新用户信息
            UserInfo byUser_id = userInfoDAO.findByUser_id(byOrder_id.getOrder_user_id());
            byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() - byGoods_id.getGoods_price() * byOrder_id.getNumber());
            byUser_id.setBeans(byUser_id.getBeans() + byGoods_id.getGoods_price() * byOrder_id.getNumber());
            userInfoDAO.updateUserInfo(byUser_id);
            //往 BeansAction中更新数据
            BeansAction beansAction = new BeansAction();
            beansAction.setBeans_action_describe("取消兑换" + byGoods_id.getGoods_name() + "的订单，平台退还。");
            beansAction.setBeans_action_number(byGoods_id.getGoods_price() * byOrder_id.getNumber());
            beansAction.setBeans_action_type(MessageConstant.BEANS_INCOME);
            beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
            beansAction.setBeans_action_user_id(byOrder_id.getOrder_user_id());
            beansActionDAO.addBeansAction(beansAction);
            //向省平台发请求更新用户鲜豆
            HttpRequestUtils.ProvinceSystemBeanAction(byOrder_id.getOrder_user_id(),byGoods_id.getGoods_price() * byOrder_id.getNumber());
            //更新订单状态信息为订单已取消
            byOrder_id.setOrder_status(MessageConstant.ORDER_CANCELED);
            ordersDAO.updateOrder(byOrder_id);
        }else if (byOrder_id.getCoupons_id() > 0 && byOrder_id.getOrder_status().equals(MessageConstant.ORDER_ALREADY_PAID)){
            List<CouponsStock> couponsStockList = couponsStockDAO.findByOrder_id(order_id);
            if (couponsStockList.size() > 0){
                for (CouponsStock couponsStock : couponsStockList) {
                    // 向 CouponsStock中更新数据
                    couponsStock.setHave_been_sold(false);
                    couponsStock.setSold_order_id(0);
                    couponsStock.setSold_time(null);
                    couponsStockDAO.updateCouponsStock(couponsStock);
                    // 向 Exchange中更新数据
                    exchangeDAO.deleteExchangeByCouponsCondition(order_id,couponsStock.getItem_id());
                }
            }
            Coupons byCoupon_id = couponsDAO.findByCoupon_id(byOrder_id.getCoupons_id());
            byCoupon_id.setStock(byCoupon_id.getStock() + byOrder_id.getNumber());
            couponsDAO.updateCoupons(byCoupon_id);
            //退还鲜豆给用户  更新用户信息
            UserInfo byUser_id = userInfoDAO.findByUser_id(byOrder_id.getOrder_user_id());
            byUser_id.setCoupons_spend_total(byUser_id.getCoupons_spend_total() - byOrder_id.getNumber());
            byUser_id.setCoupons_value_spend_total(byUser_id.getCoupons_value_spend_total() - byCoupon_id.getMarket_price() * byOrder_id.getNumber());
            byUser_id.setBeans_spend_total(byUser_id.getBeans_spend_total() - byCoupon_id.getCoupon_price() * byOrder_id.getNumber());
            byUser_id.setBeans(byUser_id.getBeans() + byCoupon_id.getCoupon_price() * byOrder_id.getNumber());
            userInfoDAO.updateUserInfo(byUser_id);
            //往 BeansAction中更新数据
            BeansAction beansAction = new BeansAction();
            beansAction.setBeans_action_describe("取消兑换" + byCoupon_id.getCoupon_name() + "的订单，平台退还。");
            beansAction.setBeans_action_number(byCoupon_id.getCoupon_price() * byOrder_id.getNumber());
            beansAction.setBeans_action_type(MessageConstant.BEANS_INCOME);
            beansAction.setBeans_action_time(new Timestamp(new Date().getTime()));
            beansAction.setBeans_action_user_id(byOrder_id.getOrder_user_id());
            beansActionDAO.addBeansAction(beansAction);
            //向省平台发请求更新用户鲜豆
            HttpRequestUtils.ProvinceSystemBeanAction(byOrder_id.getOrder_user_id(),byCoupon_id.getCoupon_price() * byOrder_id.getNumber());
            //更新订单状态信息为订单已取消
            byOrder_id.setOrder_status(MessageConstant.ORDER_CANCELED);
            ordersDAO.updateOrder(byOrder_id);
        }else {
            throw new RuntimeException("该订单已不可取消！");
        }
    }
}
